<template>
  <div class="account">
    <van-nav-bar size="20">
      <van-icon
        name="arrow-left"
        @click="onclickleft"
        size="20"
        slot="left"
        color="black"
      />
      <router-link slot="right" to="/login/phone" color="black"
        >手机号登录</router-link
      >
    </van-nav-bar>
    <div class="fonts"><span class="passfont">密码登录</span></div>
    <van-form>
      <van-cell-group inset>
        <van-field
          v-model="uname"
          name="pattern"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="upwd"
          type="password"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <van-row>
        <van-col span="16">
          <van-field
            class="code"
            name="code"
            v-model="code"
            width="100"
            size="large"
            placeholder="验证码"
            :rules="[{ required: true }]"
          />
        </van-col>
        <!-- 验证码图片  -->
        <van-col span="8">
          <van-image
            @click="change"
            fit="contain"
            class="code-img"
            width="100"
            fill
            :src="codepng"
          />
        </van-col>
      </van-row>
      <van-dialog v-model="show" title="登录成功" show-cancel-button>
      </van-dialog>
      <div style="margin: 1.5rem 1rem;">
        <van-button block type="primary" native-type="submit" @click="onSubmit">
          登录
        </van-button>
      </div>
    </van-form>
    <div>
      <div style="margin: 1.5rem 1rem;">
        <router-link to="/register">
          <van-button block type="danger" native-type="submit">
            注册
          </van-button>
        </router-link>
      </div>
      <router-link to="/" class="acolor">忘记密码</router-link>
    </div>
  </div>
</template>
<script>
import { domain } from "../a";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      uname: "",
      upwd: "",
      code: "",
      codepng: `${domain}/user/code`,
      show: false,
    };
  },
  methods: {
    ...mapMutations(["loginOk"]),
    onclickleft() {
      this.$router.go(-1);
    },
    onSubmit() {
      if (this.uname != "" && this.upwd != "" && this.code != "") {
        this.axios
          .post(
            "/user/login",
            `uname=${this.uname}&upwd=${this.upwd}&code=${this.code}`
          )
          .then((result) => {
            if (result.data.code == 200) {
              let users = result.data.data[0];
              console.log(users);
              //console.log(nick_name)
              // sessionStorage.setItem("uname",users.nick_name);
               sessionStorage.setItem("avatar",users.avatar);
              let user = JSON.stringify(users);
              sessionStorage.setItem("users", user);
              //   sessionStorage.setItem("loginOk", nick_name);
              //console.log(JSON.parse(sessionStorage.getItem("users")))
              this.loginOk(users);
              sessionStorage.setItem("islogin", true);            
              this.$toast.success("登录成功");
              this.$router.push("/");
            } else if (result.data.code == 201) {
              this.$dialog.alert({
                message: "验证码，或账号密码错误！请重新登录！",
              });
            } else {
              console.log(result);
              this.$dialog.alert({
                message: result.data.msg,
              });
              //console.log(result.data.msg)
              this.change();
            }
          });
      }
    },
    change() {
      this.codepng = `${domain}/user/code?t=` + new Date().getTime();
    },
  },
  mounted() {
    this.change();
  },
};
</script>
<style lang="scss">
.account {
  a {
    font-size: 1rem;
    color: black;
  }
  span {
    font-size: 1.5rem;
    font-weight: normal;
  }
  .fonts {
    margin: 1.4rem;
    .passfont {
      font-size: 1.5rem;
    }
  }
  .van-cell {
    height: 4rem;
    line-height: 3rem;
    padding-left: 1.3rem;
  }
  .acolor {
    margin-left: 1rem;
    color: red;
  }
  span {
    font-size: 1.2rem;
  }
}
</style>
